﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;

namespace MiniFox.Caching
{

    /// <summary>
    /// 
    /// </summary>
    internal class DefaultCacheProvider : CacheProvider
    {
        ObjectCache _cache;
        /// <summary>
        /// 
        /// </summary>
        public DefaultCacheProvider() : this(3600)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="defaultExpireSeconds"></param>
        public DefaultCacheProvider(int defaultExpireSeconds) : this(null, null, defaultExpireSeconds)
        {
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyPrefix"></param>
        /// <param name="keySuffix"></param>
        /// <param name="defaultExpireSeconds"></param>
        public DefaultCacheProvider(string keyPrefix, string keySuffix, int defaultExpireSeconds) : base(keyPrefix, keySuffix, defaultExpireSeconds)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual ObjectCache Cache
        {
            get
            {
                if (_cache == null)
                {
                    _cache = MemoryCache.Default;
                }
                return _cache;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override string GenerateKeyWithFormat(string key)
        {
            if (!string.IsNullOrWhiteSpace(this.KeySuffix))
                key = $"{key}.{this.KeySuffix}";

            return key;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeSpan"></param>
        protected override void AddItem(string key, object value, TimeSpan timeSpan)
        {
            DateTimeOffset expiration = DateTimeOffset.Now + timeSpan;
            if (!this.Cache.Add(key, value, expiration))
            {
                this.Cache.Set(key, value, expiration);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override object GetItem(string key)
        {
            return Cache.Get(key);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        protected override void RemoveItem(string key)
        {
            Cache.Remove(key);
        }
        /// <summary>
        /// 
        /// </summary>
        public override void RemoveAll()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override bool ContainItem(string key)
        {
            return Cache.Contains(key);
        }
    }
}
